package com.equipment.management.controller;


import com.equipment.management.dao.FloorDao;
import com.equipment.management.entity.Floor;
import com.equipment.management.service.FloorService;
import com.equipment.management.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/floor")
public class FloorController {

    @Autowired
    private FloorService floorService;

    @Autowired
    private FloorDao floorDao;


    @GetMapping("/all")
    public Result findAllFloor(@RequestParam(name = "floor_level", required = false)
                    Integer floor_level
            ) {
         List<Map> floor=floorService.findAllFloor(floor_level);
        return new Result(200,"调用成功",floor);
    }

    @PostMapping("/addFloor")
    public Result addFloor(@RequestBody Floor floor) throws Exception {

        Floor us = floorService.getFloorByLevel(floor.getFloor_level());
        if(us!=null){
            return new Result(300,"楼层已存在");
        }
        int i=floorService.addFloor(floor);
        floor.setCreate_time(new Date());
        return i > 0?new Result(200,"添加成功"):new Result(500,"添加失败");

    }


    @GetMapping("/deleteFloor")
    public Result deleteFloor(@RequestParam("id")int id){
        int i=floorService.deleteFloor(id);

        return i > 0?new Result(200,"删除成功"):new Result(500,"删除失败");

    }

    @PostMapping("/editFloor")
    public Result editFloor(@RequestBody Floor floor){
        int i = floorDao.editFloor(floor);
        return i > 0?new Result(200,"更新成功"):new Result(500,"更新失败");
    }

}
